package org.gwt.mosaic.samples.addressbook.client;

import org.gwt.beansbinding.core.client.ext.BeanAdapterFactory;
import org.gwt.beansbinding.ui.client.adapters.TextBoxAdapterProvider;
import org.gwt.mosaic.application.client.Application;
import org.gwt.mosaic.application.client.ApplicationConstants;
import org.gwt.mosaic.application.client.ApplicationImageBundle;
import org.gwt.mosaic.application.client.CmdAction;
import org.gwt.mosaic.application.client.SingleFrameApplication;
import org.gwt.mosaic.beansbinding.client.adapters.ListBoxAdapterProvider;
import org.gwt.mosaic.core.client.DOM;
import org.gwt.mosaic.core.client.util.DelayedRunnable;
import org.gwt.mosaic.pagebus.client.PageBus;
import org.gwt.mosaic.pagebus.client.SubscriptionCallback;
import org.gwt.mosaic.samples.addressbook.client.ui.ContactList;
import org.gwt.mosaic.samples.addressbook.client.ui.ToolBox;
import org.gwt.mosaic.ui.client.HTML;
import org.gwt.mosaic.ui.client.InfoPanel;
import org.gwt.mosaic.ui.client.MessageBox;
import org.gwt.mosaic.ui.client.WidgetWrapper;
import org.gwt.mosaic.ui.client.WindowPanel;
import org.gwt.mosaic.ui.client.MessageBox.ConfirmationCallback;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AddressBook extends SingleFrameApplication implements EntryPoint {

  private ToolBox toolBox;
  private ContactList contactList;

  private boolean mutable = false;
  private boolean searchSupport = false;

  private WindowPanel helpWindowPanel;

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    Application.launch(this,
        (ApplicationConstants) GWT.create(AddressBookConstants.class),
        (ApplicationImageBundle) GWT.create(AddressBookImageBundle.class));
  }

  @Override
  protected void initialize() {
    BeanAdapterFactory.addProvider(new TextBoxAdapterProvider());
    BeanAdapterFactory.addProvider(new ListBoxAdapterProvider());
    
    PageBus.subscribe("address-book.*", new SubscriptionCallback() {
      public void execute(String subject, Object message) {
        InfoPanel.show(subject, "Message: " + message);
      }
    });
    
    PageBus.subscribe("gwtde.*", new SubscriptionCallback() {
      public void execute(String subject, Object message) {
        InfoPanel.show(subject, "Message: " + message);
      }
    });
  }

  @Override
  protected void startup() {
    getMainView().setToolBox(toolBox = new ToolBox());
    show(contactList = new ContactList());
  }

  @Override
  protected void ready() {
    final Widget splash = RootPanel.get("splash");
    if (splash != null) {
      new DelayedRunnable() {
        @Override
        public void run() {
          DOM.setStyleAttribute(splash.getElement(), "display", "none");
        }
      };
    }
  }

  /**
   * @return the toolBox
   */
  public ToolBox getToolBox() {
    return toolBox;
  }

  /**
   * @return the contactList
   */
  public ContactList getContactList() {
    return contactList;
  }

  /**
   * @return the searchSupport
   */
  public boolean isSearchSupport() {
    return searchSupport;
  }

  /**
   * @return the mutable
   */
  public boolean isMutable() {
    return mutable;
  }

  @CmdAction(enabledProperty = "mutable")
  public void addContactCmd() {
    // TODO
  }

  @CmdAction(enabledProperty = "searchSupport")
  public void searchCmd() {
    // TODO
  }

  @CmdAction
  public void shareCmd() {
    MessageBox.confirm("Sharing options", "Share through PageBus?",
        new ConfirmationCallback() {
          public void onResult(boolean result) {
            if (result) {
              PageBus.publish("address-book.contact", "TODO: publish a real Address Book object");
            }
          }
        });
  }

  @CmdAction
  public void helpCmd() {
    if (helpWindowPanel == null) {
      helpWindowPanel = new WindowPanel("Address Book help");
      helpWindowPanel.setAnimationEnabled(true);

      helpWindowPanel.setWidget(new WidgetWrapper(
          new HTML(
              "This is an application built with <ul>"
                  + "<li><a href='http://code.google.com/p/gwt-mosaic/'>GWT Mosaic's</a> "
                  + "Application Framework (inspired by <a href='http://jcp.org/en/jsr/detail?id=296'>JSR 296</a>), "
                  + "<li><a href='http://developer.tibco.com/pagebus/default.jsp'>PageBus 1.2.0</a> & "
                  + "<li><a href='http://code.google.com/p/gwt-beans-binding/'>GWT Beans Binding</a> framework. "
                  + "</ul><p>Hopefully you don't need any real help."),
          HasAlignment.ALIGN_LEFT, HasAlignment.ALIGN_TOP));
    }

    if (!helpWindowPanel.isShowing()) {
      helpWindowPanel.setWidth("25%"); // width hint for pack
      helpWindowPanel.pack();
      helpWindowPanel.center();
    }
  }
}
